function [ op, lineXY ] = parent4polygon( sid, thrd, mgs, ORname, lineXY, saveres )
% Find parent orientation for selected polygon
%   Try to find one parent orientation for polygon selected on full EBSD
%   map. Plot all child orientation.
%
% Syntax
%   [ op, lineXY ] = parent4polygon( sid, thrd, mgs, ORname, lineXY, saveres )
%
% Output
%   ***     - ***
%
% Input
%   sid     - sample id
%   thrd    - grains detection threshold in degree
%   mgs     - minimal grains size in point
%   ORname  - name of orientation relationship
%   lineXY  - polygon coordinates
%   saveres - save result to disk
%
% History
% 12.04.13  ***


%% Reconstruction settings

% Parameters of austenite reconstruction
Nv  = 4;
PRm = 0;
w0d  = 6;
w1d  = 5;
epsd = 60;

% Convert to radian
thr = thrd*degree;
w0 = w0d*degree;
w1 = w1d*degree;

% Prepare for saving results
OutDir = checkDir(sid, 'grains', saveres);
comment = getComment();
prefix = [sid 's_0_prn_'];

%% Load EBSD
% Load EBSD data
[ ebsd_cut, lineXY ] = selectEBSD( sid, 0, 0, lineXY, saveres);

%% Detect grains
% % Calc MGS in point
% mgs = fix(mgsum/getCellArea(ebsd));

grains = getGrains(ebsd_cut('fe'), thr,mgs);
grains_fe = grains('Fe');

if (ORname == 0)
    [best_OR, x,f, ma1, ma2] = optimizeOR( sid, 0, ebsd_cut, grains_fe, saveres, thrd, mgs, epsd);
    ORname = best_OR;
end

% ng = size(grains_fe,1);
% [~, pairs] = neighbors(grains_fe);

o0 = get(grains_fe,'mean');

figure;
plot(grains);

figure;
oo = get(ebsd_cut('Fe'),'orientations');
plotAllOrientations(oo);

% figure;
% plotpdf(o0,Miller(1,0,0),'antipodal','MarkerSize',3);
% saveimg(saveres,1,OutDir,prefix, 'ori', 'png', comment);


%% Orientation relation
ORmat = getOR (ORname); % alpha to gamma

[Pmax, PR, oup, gind] = findUniqueParent(o0, ORmat, w1, Nv, w0, PRm);

Pmax
PR

if (1 || oup ~= 0)
    hold on; plotpdf(oup,Miller(1,0,0),'antipodal','MarkerSize',2, 'MarkerColor','r');
    v = getVariants(oup, inv(ORmat), get(oup,'CS'));
    hold on; plotpdf(v,Miller(1,0,0),'antipodal','MarkerSize',2, 'MarkerColor','g');
    hold off;
    saveimg(saveres,1,OutDir,prefix, 'prn', 'png', comment);
    op = oup;
else
	op = [];
end


end
